#*--------------------------------------------------------------------------------
# DESCRIPTION: Creating Appendix Table G1
#              (Entering and Exiting the 2008 Financial Crisis)
#
#--------------------------------------------------------------------------------#

install.packages(c("fect","haven", "fixest", "ggplot2", "extrafont", "dplyr", "stringr", "flextable", "officer"))

library(dplyr)
library(extrafont)
library(fect)
library(fixest)
library(ggplot2)
library(haven)
library(stringr)
library(flextable)
library(officer)

font_import()
loadfonts(device = "win")

##############################################################################################################
##SET RELEVANT WORKING DIRECTORY
setwd("...") 

##############################################################################################################
##PREPARE DATA
data <- read_dta("data/clean/country_year_panel.dta")
df.use <- data[!is.na(data$growth_shock_mpd),]
df.use <- data.frame(df.use)
df.use <- get.cohort(data = df.use, D = "growth_shock_mpd",index = c("panelid","year"))

#############################################################################################################################
##RUN MODELS

##FIRST RE-RUN CORE MODELS
model.fect <- fect(trust_govt_wo ~ growth_shock_mpd, data = df.use, 
                   method = "fe", index = c("panelid","year"), se = TRUE, nboots = 200, alpha = 0.05, 
                   parallel = TRUE, seed = 1234, force = "two-way", group = "Cohort")

model.ife <- fect(trust_govt_wo ~ growth_shock_mpd, data = df.use, 
                  index = c("panelid","year"), force = "two-way", method = "ife", CV = TRUE, r = c(0, 4), alpha = 0.05, 
                  se = TRUE, nboots = 200, parallel = TRUE, seed = 1234, group = "Cohort")

model.mc <- fect(trust_govt_wo ~ growth_shock_mpd, data = df.use, 
                 index = c("panelid","year"), force = "two-way", method = "mc", CV = TRUE, alpha = 0.05, 
                 se = TRUE, nboots = 200, parallel = TRUE, seed = 1234, group = "Cohort")

##PLACEBO TEST (Column 1)
model.fect.p <- fect(trust_govt_wo ~ growth_shock_mpd, data = df.use, 
                     index = c("panelid", "year"), force = "two-way", parallel = TRUE, se = TRUE, CV = 0, seed = 1234,
                     nboots = 200, placeboTest = TRUE, placebo.period = c(-1, 0), group = "Cohort")

fect_placebo <- plot(model.fect.p, cex.text = 0.6, stats = c("placebo.p","equiv.p"), show.group = "Cohort:2009",
                     main = "FECT: Placebo", xlim = c(-3,3), prop=F, ylim = c(-0.3, 0.3), count = F, 
                     xlab = "Time to 2008 Financial Crisis", ylab = "Effect on Trust in Government",
                     cex.lab = 0.7, cex.axis = 0.7, theme.bw = T)

model.ife.p <- fect(trust_govt_wo ~ growth_shock_mpd, data = df.use, 
                    index = c("panelid", "year"), force = "two-way", method = "ife",  r = 2, CV = 0, seed = 1234,
                    parallel = TRUE, se = TRUE, nboots = 200, placeboTest = TRUE, placebo.period = c(-1, 0), group = "Cohort")

ife_placebo <- plot(model.ife.p, cex.text = 0.6, stats = c("placebo.p","equiv.p"), show.group = "Cohort:2009",
                    main = "IFE: Placebo", xlim = c(-3,3), prop=F, ylim = c(-0.3, 0.3), count = F,
                    xlab = "Time to 2008 Financial Crisis", ylab = "Effect on Trust in Government",
                    cex.lab = 0.7, cex.axis = 0.7, theme.bw = T)

model.mc.p <- fect(trust_govt_wo ~ growth_shock_mpd, data = df.use, 
                   index = c("panelid", "year"), force = "two-way", method = "mc",  lambda = model.mc$lambda.cv, seed = 1234,
                   CV = 0, parallel = TRUE, se = TRUE, nboots = 200, placeboTest = TRUE, placebo.period = c(-1, 0), group = "Cohort")

mc_placebo <- plot(model.mc.p, cex.text = 0.6, stats = c("placebo.p","equiv.p"), show.group = "Cohort:2009",
                   main = "MC: Placebo", xlim = c(-3,3), prop=F, ylim = c(-0.3, 0.3), count = F,
                   xlab = "Time to 2008 Financial Crisis", ylab = "Effect on Trust in Government",
                   cex.lab = 0.7, cex.axis = 0.7, theme.bw = T)

##PRE-TREND TEST (Column 2)
fect_pretrend <- plot(model.fect, type = "equiv", cex.text = 0.7, show.group = "Cohort:2009",
                      main = "FECT: Pre-Trends", xlim = c(-3,0), prop=F, ylim = c(-0.3, 0.3), count = F,
                      xlab = "Time to 2008 Financial Crisis", ylab = "Effect on Trust in Government",
                      cex.lab = 0.7, cex.axis = 0.7, theme.bw = T, legendOff = T)

ife_pretrend <- plot(model.ife, type = "equiv", cex.text = 0.7, show.group = "Cohort:2009",
                     main = "IFE: Pre-Trends", xlim = c(-3,0), prop=F, ylim = c(-0.3, 0.3), count = F,
                     xlab = "Time to 2008 Financial Crisis", ylab = "Effect on Trust in Government",
                     cex.lab = 0.7, cex.axis = 0.7, theme.bw = T, legendOff = T)

mc_pretrend <- plot(model.mc, type = "equiv", cex.text = 0.7, show.group = "Cohort:2009",
                    main = "MC: Pre-Trends", xlim = c(-3,0), prop=F, ylim = c(-0.3, 0.3), count = F,
                    xlab = "Time to 2008 Financial Crisis", ylab = "Effect on Trust in Government",
                    cex.lab = 0.7, cex.axis = 0.7, theme.bw = T, legendOff = T)

##CARRYOVER TEST (Column 3)
model.fect.c <- fect(trust_govt_wo ~ growth_shock_mpd, data = df.use, 
                     index = c("panelid", "year"), force = "two-way", parallel = TRUE, se = TRUE, CV = 0, seed = 1234,
                     nboots = 200, carryoverTest = TRUE, carryover.period = c(1, 1), group = "Cohort")

fect_carryover <- plot(model.fect.c, type = "exit", cex.text = 0.6, stats = c("carryover.p","equiv.p"), show.group = "Cohort:2009",
                       main = "FECT: Carryover", xlim = c(-2,4), prop=F, ylim = c(-0.3, 0.3), count = F,
                       xlab = "Time to Exiting 2008 Financial Crisis", ylab = "Effect on Trust in Government",
                       cex.lab = 0.7, cex.axis = 0.7, theme.bw = T)

model.ife.c <- fect(trust_govt_wo ~ growth_shock_mpd, data = df.use, 
                    index = c("panelid", "year"), force = "two-way", method = "ife", r = 2, CV = 0, seed = 1234,
                    parallel = TRUE, se = TRUE, nboots = 200, carryoverTest = TRUE, carryover.period = c(1, 1), group = "Cohort")

ife_carryover <- plot(model.ife.c, cex.text = 0.6, stats = c("carryover.p","equiv.p"), show.group = "Cohort:2009",
                      main = "IFE: Carryover", xlim = c(-2,4), prop=F, ylim = c(-0.3, 0.3), count = F,
                      xlab = "Time to Exiting 2008 Financial Crisis", ylab = "Effect on Trust in Government",
                      cex.lab = 0.7, cex.axis = 0.7, theme.bw = T)

model.mc.c <- fect(trust_govt_wo ~ growth_shock_mpd, data = df.use, 
                   index = c("panelid", "year"), force = "two-way", method = "mc",  lambda = model.mc$lambda.cv, seed = 1234,
                   CV = 0, parallel = TRUE, se = TRUE, nboots = 200, carryoverTest = TRUE, carryover.period = c(1, 1), group = "Cohort")

mc_carryover <- plot(model.mc.c, cex.text = 0.6, stats = c("carryover.p","equiv.p"), show.group = "Cohort:2009",
                     main = "MC: Carryover", xlim = c(-2,4), prop=F, ylim = c(-0.3, 0.3), count = F,
                     xlab = "Time to Exiting 2008 Financial Crisis", ylab = "Effect on Trust in Government",
                     cex.lab = 0.7, cex.axis = 0.7, theme.bw = T)

######################
##Create Table G1##
######################

fect_placebo_vals <- fect_placebo[["plot_env"]][["stats_display"]]
ife_placebo_vals  <- ife_placebo[["plot_env"]][["stats_display"]]
mc_placebo_vals   <- mc_placebo[["plot_env"]][["stats_display"]]

fect_pre_vals <- fect_pretrend[["plot_env"]][["stats_display"]]
ife_pre_vals  <- ife_pretrend[["plot_env"]][["stats_display"]]
mc_pre_vals   <- mc_pretrend[["plot_env"]][["stats_display"]]

fect_carry_vals <- fect_carryover[["plot_env"]][["stats_display"]]
ife_carry_vals  <- ife_carryover[["plot_env"]][["stats_display"]]
mc_carry_vals   <- mc_carryover[["plot_env"]][["stats_display"]]

table_g1 <- data.frame(
  Model = c("FECT", "", "", "IFE", "", "", "MC", "", ""),
  Placebo = c(
    paste0("(", fect_placebo_vals[1], ")"),
    paste0("[", fect_placebo_vals[2], "]"),
    "",
    
    paste0("(", ife_placebo_vals[1], ")"),
    paste0("[", ife_placebo_vals[2], "]"),
    "",
    
    paste0("(", mc_placebo_vals[1], ")"),
    paste0("[", mc_placebo_vals[2], "]"),
    ""
  ),
  PreTrends = c(
    paste0("(", fect_pre_vals[1], ")"),
    paste0("[", fect_pre_vals[2], "]"),
    "",
    
    paste0("(", ife_pre_vals[1], ")"),
    paste0("[", ife_pre_vals[2], "]"),
    "",
    
    paste0("(", mc_pre_vals[1], ")"),
    paste0("[", mc_pre_vals[2], "]"),
    ""
  ),
  Carryover = c(
    paste0("(", fect_carry_vals[1], ")"),
    paste0("[", fect_carry_vals[2], "]"),
    "",
    
    paste0("(", ife_carry_vals[1], ")"),
    paste0("[", ife_carry_vals[2], "]"),
    "",
    
    paste0("(", mc_carry_vals[1], ")"),
    paste0("[", mc_carry_vals[2], "]"),
    ""
  ),
  check.names = FALSE
)

ft_g1 <- flextable(table_g1)
ft_g1 <- autofit(ft_g1)

doc <- read_docx()
doc <- body_add_flextable(doc, ft_g1)

print(doc, target = "output/tables/appendix_g/table_g1.docx")
